<?php
session_start();
require_once '../confirmarAdmin.php';
require_once '../database.php';
require_once '../formValidation.php';
$db = new DBAdminConnection();

if(isset($_POST['problema'])){
    $idAutor = $_SESSION['idUser'];
    $idProblema = $_POST['problema'];
    $dataInicio = $_POST['dataInicio'];
    $dataFim = $_POST['dataFim'];
    
     $valid = FieldValidator::validateNumber($idProblema) &&
            FieldValidator::validateDate($dataInicio) &&
            FieldValidator::validateDate($dataFim);
     
      if(!$valid){
        header("Location: http://localhost/Matematrix/admin/jornada.php?invalid=true");
        exit();
    }
    
    $res = $db->adicionarJornada($idAutor, $idProblema, $dataInicio, $dataFim);
    if($res){
        header("Location: http://localhost/Matematrix/admin/lista.php?type=jornadas");
    } else {
        header("Location: http://localhost/Matematrix/admin/jornada.php?insert=false");
    }
    exit();
}
    $problemas = $db->getProblemasDisponiveis();
    require_once 'view/formJornada.php';

?>
